/**
 * 中国地区数据
 * 包含省份、城市、区县的层级结构
 */

export interface RegionNode {
  id: string;
  name: string;
  children?: RegionNode[];
}

// 模拟中国地区数据
export const chinaRegions: RegionNode[] = [
  {
    id: '11',
    name: '北京市',
    children: [
      {
        id: '1101',
        name: '北京市',
        children: [
          { id: '110101', name: '东城区' },
          { id: '110102', name: '西城区' },
          { id: '110105', name: '朝阳区' },
          { id: '110106', name: '丰台区' },
          { id: '110107', name: '石景山区' },
          { id: '110108', name: '海淀区' },
          { id: '110109', name: '门头沟区' },
          { id: '110111', name: '房山区' }
        ]
      }
    ]
  },
  {
    id: '31',
    name: '上海市',
    children: [
      {
        id: '3101',
        name: '上海市',
        children: [
          { id: '310101', name: '黄浦区' },
          { id: '310104', name: '徐汇区' },
          { id: '310105', name: '长宁区' },
          { id: '310106', name: '静安区' },
          { id: '310107', name: '普陀区' },
          { id: '310109', name: '虹口区' },
          { id: '310110', name: '杨浦区' },
          { id: '310115', name: '浦东新区' }
        ]
      }
    ]
  },
  {
    id: '33',
    name: '浙江省',
    children: [
      {
        id: '3301',
        name: '杭州市',
        children: [
          { id: '330102', name: '上城区' },
          { id: '330103', name: '下城区' },
          { id: '330104', name: '江干区' },
          { id: '330105', name: '拱墅区' },
          { id: '330106', name: '西湖区' },
          { id: '330108', name: '滨江区' },
          { id: '330110', name: '余杭区' }
        ]
      },
      {
        id: '3302',
        name: '宁波市',
        children: [
          { id: '330203', name: '海曙区' },
          { id: '330204', name: '江东区' },
          { id: '330205', name: '江北区' },
          { id: '330206', name: '北仑区' }
        ]
      }
    ]
  },
  {
    id: '32',
    name: '江苏省',
    children: [
      {
        id: '3201',
        name: '南京市',
        children: [
          { id: '320102', name: '玄武区' },
          { id: '320104', name: '秦淮区' },
          { id: '320105', name: '建邺区' },
          { id: '320106', name: '鼓楼区' },
          { id: '320111', name: '浦口区' }
        ]
      },
      {
        id: '3205',
        name: '苏州市',
        children: [
          { id: '320508', name: '姑苏区' },
          { id: '320509', name: '虎丘区' },
          { id: '320510', name: '吴中区' },
          { id: '320512', name: '相城区' }
        ]
      }
    ]
  },
  {
    id: '44',
    name: '广东省',
    children: [
      {
        id: '4401',
        name: '广州市',
        children: [
          { id: '440103', name: '荔湾区' },
          { id: '440104', name: '越秀区' },
          { id: '440105', name: '海珠区' },
          { id: '440106', name: '天河区' }
        ]
      },
      {
        id: '4403',
        name: '深圳市',
        children: [
          { id: '440303', name: '罗湖区' },
          { id: '440304', name: '福田区' },
          { id: '440305', name: '南山区' },
          { id: '440306', name: '宝安区' }
        ]
      }
    ]
  },
  {
    id: '51',
    name: '四川省',
    children: [
      {
        id: '5101',
        name: '成都市',
        children: [
          { id: '510104', name: '锦江区' },
          { id: '510105', name: '青羊区' },
          { id: '510106', name: '金牛区' },
          { id: '510107', name: '武侯区' }
        ]
      }
    ]
  },
  {
    id: '42',
    name: '湖北省',
    children: [
      {
        id: '4201',
        name: '武汉市',
        children: [
          { id: '420102', name: '江岸区' },
          { id: '420103', name: '江汉区' },
          { id: '420104', name: '硚口区' },
          { id: '420105', name: '汉阳区' }
        ]
      }
    ]
  }
];

/**
 * 获取城市对应的行政区列表
 * @param provinceId 省份ID
 * @param cityId 城市ID
 * @returns 行政区列表
 */
export const getCityDistricts = (provinceId: string, cityId: string) => {
  const province = chinaRegions.find(p => p.id === provinceId);
  if (!province) return [];
  
  const city = province.children?.find(c => c.id === cityId);
  if (!city) return [];
  
  return city.children?.map(d => ({
    id: d.id,
    name: d.name,
    parents: [provinceId, cityId]
  })) || [];
};

/**
 * 根据城市代码获取城市名称
 * @param provinceId 省份ID
 * @param cityId 城市ID
 * @returns 城市名称
 */
export const getCityNameById = (provinceId: string, cityId: string): string => {
  const province = chinaRegions.find(p => p.id === provinceId);
  if (!province) return '';
  
  const city = province.children?.find(c => c.id === cityId);
  if (!city) return '';
  
  return city.name;
};